#include <lpc23xx.h>
#include "LCD.h"
unsigned  int *StandardFilter; 								//Declare pointers for the acceptence filter tables 
unsigned  int *GroupStdFilter; 		
unsigned  int *IndividualExtFilter;    
unsigned  int *GroupExtFilter;		

void CAN1_Config(void);
void CAN1IRQ (void)__irq;
unsigned int flasher;

int main(void)
{
unsigned int  val = 0x55555555;
    
unsigned int delay; //
	/* Reset all GPIO pins to default: primary function */
    PINSEL0 = 0x00000000;
    PINSEL1 = 0x00000000;
    PINSEL2 = 0x00000000;
    PINSEL3 = 0x00000000;
    PINSEL4 = 0x00000000;
    PINSEL5 = 0x00000000;
    PINSEL6 = 0x00000000;
    PINSEL7 = 0x00000000;
    PINSEL8 = 0x00000000;
    PINSEL9 = 0x00000000;
    PINSEL10 = 0x00000000;
    
    IODIR0 = 0x00000000;
    IODIR1 = 0x00000000;
	  IOSET0 = 0x00000000;
    IOSET1 = 0x00000000;
    
    FIO0DIR = 0x00000000;
    FIO1DIR = 0x00000000;
    FIO2DIR = 0x00000000;
    FIO3DIR = 0x00000000;
    FIO4DIR = 0x00000000;
    
    FIO0SET = 0x00000000;
    FIO1SET = 0x00000000;
    FIO2SET = 0x00000000;
    FIO3SET = 0x00000000;
    FIO4SET = 0x00000000;					
    FIO2DIR = 0x000000FF;
    FIO2MASK = 0xFFFFFF00; 
    
    PINSEL0               |=   0x2<<8|0x2<<10;// enable the CAN2TD and RD pins
    PCONP                 |=   0x1<<14;       //Power up the CAn 2 controller
    
    CAN2MOD 	            |=  0x00000001;			//Set CAN controller into reset
    CAN2BTR 	             = 	0x0B | 0x05<<16 | 0x02<< 20;//Configure the bit timing
    CAN2MOD 	             = 	0x00000000;			//Release CAN controller
    CAN1_Config();

	 lcd_init();					   //
	lcd_clear();				  //
	set_cursor(1,0);			   // Agaf=/
    lcd_print("CANRx Example");	 //    
while(1)
{

    CAN2TFI1 	 	        = 		0x00040000;			//Set DLC to 4 bytes 	

 
    for(flasher =1;flasher<0x00000100;flasher = flasher <<1)
    {
      CAN2TID1 		= 		flasher;			//Set address to 2 Standard Frame
      
       if(CAN2SR & 0x00000004)					//See if Tx Buffer 1 is free
        {

          CAN2TDA1		=		flasher;				//Copy A/D result into first four bytes
          CAN2CMR 		= 		0x00000021;			//Transmit the message
        }
      
        for(delay = 0;delay<0xA0000;delay++)				  	       //simple delay loop
        {
          ;
        }
      }
    FIO2CLR = 0x000000FF;


}

}

void CAN1IRQ (void)	__irq  
{
    FIO2CLR               = 0x000000FF;
    FIO2SET               = CAN1RDA;
    CAN1CMR 			  =	0x00000004; 					//release the recieve buffer
    VICVectAddr 		  =	0x00000000;						//Signal the end of interrupt
}

void CAN1_Config(void)
  {
    PINSEL0                 |=  0x5;
    PCONP                   |=  0x1<<13 ;
    
    CAN1MOD 	           |= 0x00000001;			      //Set CAN controller into reset
    CAN1BTR 	            = 0x0B | 0x05<<16 | 0x02<< 20;
    CAN1IER	            =	0x00000001;						//Enable the Receive interrupt 
    
    VICVectPriority23	    = 0x00000000;  					//select a priority slot for a given interrupt
    VICVectAddr23 		    = (unsigned)CAN1IRQ;				//pass the address of the IRQ into the VIC slot
    VICIntEnable 		    = 0x00800000;						//enable interrupt

    CAN_AFMR					    =	0x00000003;						//Disable the Acceptance filters to allow setup of the table
/*
    StandardFilter			  = (unsigned int *)0xE0038000;		//Pointer to Standard filter table
    *StandardFilter			  =	0x00020003;
    StandardFilter++;
    *StandardFilter			  =	0x00040005;

    GroupStdFilter			  = (unsigned int *)0xE0038008;   	//Pointer to Group filter table
    *GroupStdFilter			  =	0x0009000F;
    GroupStdFilter++;
    *GroupStdFilter			  =	0x00110020;

    IndividualExtFilter		= (unsigned int *)0xE0038010;   	//Pointer to Extended filter table
    *IndividualExtFilter 	= 0x40010000;
    IndividualExtFilter++;
    *IndividualExtFilter 	= 0x40020000;

    GroupExtFilter			  = (unsigned int *)0xE0038018;   	//Pointer to Eternded Group filter table
    *GroupExtFilter			  =	0x40030000;
    GroupExtFilter++;
    *GroupExtFilter			  =	0x40040000;

    CAN_SFF_SA					  =	0x00000000;			 			//Set start address of Standard table
    CAN_SFF_GRP_SA				=	0x00000008;						//Set start address of Standard group table
    CAN_EFF_SA					  =	0x00000010;						//Set start address of Extended table
    CAN_EFF_GRP_SA				=	0x00000018;						//Set start address of Extended group table
    CAN_EOT     				  =	0x00000020;						//Set end of table address
    CAN_AFMR					    =	0x00000000;						//Enable Acceptance filters
*/
    CAN1MOD 					    = 0x00000000;						//Release CAN controller

}